﻿using com.nm.ApiServer;
using com.nm.ApiServer.Attributes;
using com.nm.Attributes;
using com.nm.db.Models.Systems;
using com.nm.library.Helper;
using EntityFramework.Extensions;
using NHibernateVertica;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseAPI
{
    [Description("用户店铺权限管理")]
    public class UserShopHandler : BaseHandler
    {
        public VerticaDataHelper Helper { get; set; } = new VerticaDataHelper();

        [MustLogin]
        [Description("所有店铺列表")]
        [ReturnType(typeof(IList<TreeData>))]
        public ApiResult GetAllShopList()
        {
            //省份
            var ParnetArea = GetAreaList(1).OrderBy(o => o.NodeID);
            var Aera = GetAreaList(2).OrderBy(o => o.NodeID); ;
            var Shop = GetList("select AreaID as ParentID,ShopID as NodeID,ShopName as NodeName from dw.dw_Shop").OrderBy(o => o.NodeID); ;
            var result = ParnetArea.Select(o => new TreeData
            {
                NodeID = o.NodeID,
                NodeName = o.NodeName,
                Childs = Aera.Where(b => b.ParentID == o.NodeID).Select( c => new TreeData {
                    NodeID = c.NodeID,
                    NodeName = c.NodeName,
                    Childs = Shop.Where(b => b.ParentID == c.NodeID).Select(s => new TreeData {
                        NodeID = s.NodeID,
                        NodeName = s.NodeName
                    }).ToList()
                }).ToList()
            });
            return Success(result);
        }
        /// <summary>
        /// 城市
        /// </summary>
        /// <param name="ParentID"></param>
        /// <returns></returns>
        protected IList<ListData> GetAreaList(int Level)
        {
            var sql = $@"select ParentID,AreaID as NodeID,AreaName as NodeName 
                         from ods.ods_erp_Bas_Area  where Level ={Level}";
            var dt = GetData(sql);
            var qurey = ModelConvertHelper<ListData>.ConvertToModel(dt).ToList();
            var result = qurey.Select(o => new ListData
            {
                ParentID = o.ParentID,
                NodeID = o.NodeID,
                NodeName = o.NodeName
                //Childs = GetShopList(o.NodeID)
            }).ToList();
            return result;
        }
        
        protected IList<ListData> GetList(string sql)
        {
            var dt = GetData(sql);
            var qurey = ModelConvertHelper<ListData>.ConvertToModel(dt).ToList();
            var result = qurey.Select(o => new ListData
            {
                ParentID = o.ParentID,
                NodeID = o.NodeID,
                NodeName = o.NodeName
                //Childs = GetShopList(o.NodeID)
            }).ToList();
            return result;
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        protected DataTable GetData(string sql)
        {
            var cmd = Helper.GetSqlStringCommand(sql);
            var dt = Helper.ExecuteDataTable(cmd);
            return dt;
        }
        [MustLogin]
        [Description("已选店铺列表")]
        [ReturnType(typeof(IList<CheckData>))]
        public ApiResult GetUserShopList([ParamDescription("用户ID")]int UserID)
        {
            //选中值
            var dc = new SysEntities();
            var result = dc.sys_userData_rights.Where(t => t.UserID == UserID).Select(o => new CheckData
            {
                Value = o.DataValue
            }).ToList();
            return Success(result);
        }
       
        [JsonType]
        [MustLogin]
        [Description("保存数据")]
        [ReturnType(typeof(int), "0-成功")]
        public ApiResult Save([ParamDescription("修改数据")]ModifyData param)
        {
            if (param == null) return Error("参数对象不能为空！");
            var dc = new SysEntities();
            dc.sys_userData_rights.Where(t => t.UserID == param.UserID).Delete();
            if (param.ShopList != null)
            {
                foreach (var item in param.ShopList)
                {
                    var shop = new sys_userData_rights
                    {
                        UserID = param.UserID,
                        DataType = 0,
                        CreateDate = DateTime.Now,
                        DataValue = item,
                        Operator = this.UserId
                    };
                    dc.sys_userData_rights.Add(shop);
                }
            }
            try
            {
                dc.SaveChanges();
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
            return Success(0);
        }


        public class ListData : NodeData
        {
            [ParamDescription("父节点")]
            public string ParentID { get; set; }
        }
        public class NodeData
        {
            [ParamDescription("节点ID")]
            public string NodeID { get; set; }
            [ParamDescription("节点名称")]
            public string NodeName { get; set; }
        }
        public class TreeData : NodeData
        {
            [ParamDescription("子节点列表")]
            public IList<TreeData> Childs { get; set; }
        }
        [Description("选中数据")]
        public class CheckData
        {
            [ParamDescription("值")]
            public string Value { get; set; }
        }

        [Description("修改数据")]
        public class ModifyData
        {
            [ParamDescription("用户ID")]
            public int UserID { get; set; }
            [ParamDescription("店铺列表")]
            public IList<string> ShopList { get; set; }
        }
    }

   
}
